package com.ludi.basics.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author ludi
 * @date:2020/8/21 20:21
 * @description:打印日志
 */
@Aspect
@Component
public class AnnotationAdvice {

    @Pointcut("execution(* com.ludi.basics.controller.*Controller.*(..))")
    public void pointCut(){}
    @Before("AnnotationAdvice.pointCut()")
    public void before(JoinPoint point){
        System.out.println("target:"+point.getTarget());
        System.out.println("args:"+point.getArgs());
        System.out.println("method's name:"+point.getSignature().getName());
        System.out.println("before~~~~");
    }

//    @After("AnnotationAdvice.pointCut()")
//    public void after(JoinPoint point){
//        System.out.println("after~~~~");
//    }


    //后置通知(如果出现异常不会调用)
    @AfterReturning(value = "AnnotationAdvice.pointCut()",returning = "ret")
    public void afterReturning(JoinPoint point,Object ret) {
        System.out.println("after~~~~:"+ret);
    }

//    //环绕通知
//    @Around("AnnotationAdvice.pointCut()")
//    public Object around(ProceedingJoinPoint point) throws Throwable {
//        System.out.println("这是环绕通知之前部分!!");
//        Object object = point.proceed(); //调用目标方法
//        System.out.println("这是环绕通知之后的部分!!");
//        return object;
//    }

    //异常拦截通知
    @AfterThrowing(pointcut = "AnnotationAdvice.pointCut()",throwing = "ex")
    public void afterException(JoinPoint point,Throwable ex) {

        System.out.println("出现异常");
        System.out.println(ex.getMessage());
    }
}
